// const { default: $ } = require("wangeditor/dist/utils/dom-core")
// const center = require("../../../router/home/center")

window.onload = function () {
    var sex = document.getElementById("hidden").innerHTML
    var radios = document.getElementsByClassName("radio")
    console.log(sex)
    if(sex == 1){
        radios[0].checked = true
        radios[1].checked = false
    }else{
        radios[1].checked = true
        radios[0].checked = false
    }
    document.getElementById("message").onclick = function () {
        var a = document.querySelectorAll('dl>dd')
        for(i=0;i<4;i++){
            console.log(a[i])
            a[i].classList.toggle("hide")
        }
    }
    document.getElementById("superSail").onclick = function () {
        var a = document.querySelectorAll('dl>dd')
        for(i=4;i<6;i++){
            console.log(a[i])
            a[i].classList.toggle("hide")
        }
    }
    document.getElementById("order").onclick = function () {
        var a = document.querySelectorAll('dl>dd')
        for(i=6;i<a.length;i++){
            a[i].classList.toggle("hide")
        }
    }
    var change=document.getElementById("change");
    change.onclick=function(){
        var sex;
        var telephone=document.getElementById("phone").value;
        // var address=document.getElementById("address").value;
        if (document.getElementsByClassName("radio")[0].checked){
            sex="男";
        }else if(document.getElementsByClassName("radio")[1].checked){
            sex="女";
        }
        var nickname = document.getElementById("nickname").value;
        var birth = document.getElementById("year").value+`.`+document.getElementById("month").value+`.`+document.getElementById("day").value
        var reg=/^(13|14|15|17|18)[0-9]{9}/;
         if(reg.test(telephone)&&telephone.length==11){
             console.log(2)
            axios.get('/home/center/user/updateInformation',{
                params:{
                    telephone:telephone,
                    nickname:nickname,
                    sex:sex,
                    birth:birth
                }
            })
            .then(function(data){
                console.log("成功")
                console.log(data.data.data.code)
                if(data.data.data.code == 20000){
                    uploadPhoto();
                }
            })
            .catch(function(error){
                console.log(3)
                console.log(error);
                document.getElementById("floatContent").innerHTML ="<div>"+"修改失败，请联系工作人员"+"</div>"+"<button onclick='closethis()' class='newbtn'>"+"确定"+"</button>";
                document.getElementById("floatContent").classList.remove("hide")
                // document.getElementById("shadow").style.display='block'
            })
        }else{
            document.getElementById("floatContent").innerHTML ="<div>"+"提示\n\n请输入有效的电话号码！"+"</div>"+"<button onclick='closethis()' class='newbtn'>"+"确定"+"</button>";
            document.getElementById("floatContent").classList.remove("hide")
            // document.getElementById("shadow").style.display='block'
         }
    };
    // document.getElementById("oImg").change = function() {
    //     console.log(10086)
    //     var $file = $(this);
    //     var objUrl = $file[0].files[0];
    //     //获得一个http格式的url路径:mozilla(firefox)||webkit or chrome
    //     var windowURL = window.URL || window.webkitURL;
    //     //createObjectURL创建一个指向该参数对象(图片)的URL
    //     var dataURL;
    //     dataURL = windowURL.createObjectURL(objUrl);
    //     //把url给图片的src，让其显示
    //     console.log("-------------------")
    //     console.log(dataURL)
    //     console.log("-------------------")
    // }
}
function selectMonth() {
    var month = document.getElementById("month");
    var value = month.value;
    if(value%2==1){
        document.getElementById("day").innerHTML = ``
        for(i=1;i<=31;i++){
            document.getElementById("day").innerHTML += `<option value="1">`+i+`</option>`
        }
    }else if(value == 2){
        document.getElementById("day").innerHTML = ``
        for(i=1;i<=28;i++){
            document.getElementById("day").innerHTML += `<option value="1">`+i+`</option>`
        }
    }else{
        document.getElementById("day").innerHTML = ``
        for(i=1;i<=30;i++){
            document.getElementById("day").innerHTML += `<option value="1">`+i+`</option>`
        }
    }
}
function previewFile() {
    var oImg = document.getElementById('oImg');
    var file = document.getElementById('ipt').files[0];
    // console.log(file)
    var reader = new FileReader();
    
    reader.addEventListener('load', function() {
        oImg.src = this.result;
        img = oImg.src;
    }, false);
    if (file) {
        reader.readAsDataURL(file);
    }
}

function uploadPhoto(){
    // console.log(img)
    // axios.put('/home/center/user/uploadHeadImg',{
    //     params:{                
    //         img:img,
    //         userId:15
    //     }
    // }).then(function(data){
    //     if (data.data.data.code == 20000) {
    //         var list = data.data;
    //         console.log(list)
    //         document.getElementById("floatContent").innerHTML =`<div>${data.data.data.data}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
    //         document.getElementById("floatContent").classList.remove("hide");
    //     } else {
    //         document.getElementById("floatContent").innerHTML = `<div>${data.data.data.data}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
    //         document.getElementById("floatContent").classList.remove("hide");
    //     }
    // }).catch(function(error){
    //     console.log(error)
    //     document.getElementById("floatContent").innerHTML =`<div>${error}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
    //     document.getElementById("floatContent").classList.remove("hide");
    // })
    var userId = document.getElementById("di").innerHTML
    img = document.getElementById('oImg').src
    var file = document.getElementById('ipt');
    console.log(10086)
    console.log(file);
    var formData = new FormData()
    console.log(formData)
    formData.append('multipartFile',file.files[0]);
    console.log(formData)
    formData.append('myfile',file.files[0]);
    console.log(formData)
    axios({
        url: `/home/center/user/uploadHeadImg`,
        data: formData,
        params:{
            userId:userId
        },
        method: 'put'
    }).then((data) => {
        console.log(10085)
        console.log(data)   
        document.getElementById("floatContent").innerHTML =`<div>${data.data.data.data}</div><button onclick='closethis()' class='newbtn'>确定</button>`;
        document.getElementById("floatContent").classList.remove("hide");
    }).catch((err) => {
        console.log(err)
    })
}
function closethis(){
    document.getElementById("floatContent").classList.add("hide");
}